package com.gr.catanddog_web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gr.catanddog_common.aspect.ApiOperationLog;
import com.gr.catanddog_common.damin.Content;
import com.gr.catanddog_common.damin.ContentClick;
import com.gr.catanddog_common.damin.User;
import com.gr.catanddog_common.dao.ContentClickMapper;
import com.gr.catanddog_common.dao.ContentImageMapper;
import com.gr.catanddog_common.dao.ContentMapper;
import com.gr.catanddog_common.dao.UserMapper;
import com.gr.catanddog_common.utils.Response;
import com.gr.catanddog_common.utils.VerifyCode;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 大睡大唬
 * @version 1.0
 * @description: TODO
 * @date 2024/3/5 20:27
 */
@RestController
@Slf4j
@MultipartConfig()
/*@CrossOrigin(origins ="*",maxAge = 3600,allowCredentials = "true",allowedHeaders = "token")*/
public class UserController {
    @Value("${web.url.path}")
    String webPath;
    @Autowired
    ContentMapper contentMapper;
    @Autowired
    ContentImageMapper contentImageMapper;
    @Autowired
    ContentClickMapper contentClickMapper;

    @Value("${file.upload.url}")
    private  String filePath;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    SimpleDateFormat formatter;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VerifyCode verifyCode;

    @GetMapping("getUserinfo")
    @ApiOperationLog(description = "个人信息")
    public Response gerUserinfo(HttpServletRequest request){

        //String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiLlpKfnnaHlpKfllKwiLCJleHAiOiIxNzA5MDkwOTE5MjI1IiwiaWF0IjoiMTcwOTA5MDkxNTYyNSJ9.CccsWEEzS/cPKatkjCT9+Yl9m5KX2Uf+n3Y2Z/XQFXU=";
        String token = request.getHeader("token");
        System.out.println(token);
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
        lqw.eq(User::getToken,token);
        User user = userMapper.selectOne(lqw);
        LambdaQueryWrapper<ContentClick> lqw12 = new LambdaQueryWrapper<>();
        lqw12.eq(ContentClick::getUserToken,token)
                .eq(ContentClick::getIsCollect,true);
        List<ContentClick> contentClicks = contentClickMapper.selectList(lqw12);
        List<String> contentId = new ArrayList<>();
        for (ContentClick contentClick : contentClicks) {
            contentId.add(contentClick.getContentId());
        }
        List<Content> contents = new ArrayList<>();
        if(!contentId.isEmpty())
        {
            LambdaQueryWrapper<Content> lqw1 = new LambdaQueryWrapper<>();
            lqw1.in(Content::getId, contentId);
            contents = contentMapper.selectList(lqw1);
        }
        user.setCollectContents(contents);
        LambdaQueryWrapper<Content> lqw11 = new LambdaQueryWrapper<>();
        lqw11.eq(Content::getUserToken,token);
        List<Content> contents1 = contentMapper.selectList(lqw11);
        user.setContents(contents1);
        Integer i = 0;
        for (Content content : contents1) i += content.getLikes();
        user.setLikes(i);
        return  Response.success(user);
    }
    @PostMapping ("save")
    @ApiOperationLog(description = "个人信息保存")
    public Response saveUserinfo(HttpServletRequest request,
                                 @RequestParam("username")String username,
                                 @RequestParam("phone")String phone,
                                 @RequestParam(value = "file",required = false)MultipartFile file) throws IOException {
        //String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiLlpKfnnaHlpKfllKwiLCJleHAiOiIxNzA5MDkwOTE5MjI1IiwiaWF0IjoiMTcwOTA5MDkxNTYyNSJ9.CccsWEEzS/cPKatkjCT9+Yl9m5KX2Uf+n3Y2Z/XQFXU=";
        String token = request.getHeader("token");
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
        lqw.eq(User::getToken,token);
        User user = userMapper.selectOne(lqw);
        user.setUserName(username);
        user.setPhone(phone);
        String fileName = String.valueOf(new Date().getTime())+".jpg";
        if(file!=null){
            File dest = new File(filePath+'/'+fileName);
            file.transferTo(dest);
            user.setHeadPortrait(webPath+fileName);
        }
        userMapper.update(user, lqw);
        return Response.success();
    }

    @GetMapping("getCollect")
    @ApiOperationLog(description = "我的收藏")
    public Response getCollect(HttpServletRequest request){
        //String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiLlpKfnnaHlpKfllKwiLCJleHAiOiIxNzA5MDkwOTE5MjI1IiwiaWF0IjoiMTcwOTA5MDkxNTYyNSJ9.CccsWEEzS/cPKatkjCT9+Yl9m5KX2Uf+n3Y2Z/XQFXU=";
        String token = request.getHeader("token");
        LambdaQueryWrapper<ContentClick> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ContentClick::getUserToken,token)
                .eq(ContentClick::getIsCollect,true);
        List<ContentClick> contentClicks = contentClickMapper.selectList(lqw);
        List<String> contentId = new ArrayList<>();
        for (ContentClick contentClick : contentClicks) {
            contentId.add(contentClick.getContentId());
        }
        LambdaQueryWrapper<Content>lqw1 = new LambdaQueryWrapper<>();
        lqw1.in(Content::getId,contentId);
        List<Content> contents = contentMapper.selectList(lqw1);
        return Response.success(contents);
    }

    @GetMapping("myContent")
    @ApiOperationLog(description = "我的发布")
    public  Response getMyContent(HttpServletRequest request){
        String token = request.getHeader("token");
        LambdaQueryWrapper<Content> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Content::getUserToken,token);
        List<Content> contents = contentMapper.selectList(lqw);
        return  Response.success(contents);
    }

    @GetMapping("isOne")
    @ApiOperationLog(description = "用户名是否唯一")
    public Response isOne(@RequestParam("userName")String userName){
        LambdaQueryWrapper<User> lqw =new LambdaQueryWrapper<>();
        lqw.eq(User::getUserName,userName);
        User user = userMapper.selectOne(lqw);
        if(user!=null){
            return Response.fail();
        }else return Response.success();
    }
}
